/**
 * 
 */
package com.lgCore.netty.codec.impl;

import com.esotericsoftware.kryo.io.Output;
import com.lgCore.netty.codec.CommonRpcEncoder;
import com.lgCore.util.KryoUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @author ligang
 *
 */
public class KryoEncoder implements CommonRpcEncoder {

	/* (non-Javadoc)
	 * @see com.jd.cross.plateform.rocketrpc.core.codec.RocketRPCEncoder#encode(java.lang.Object)
	 */
	@Override
	public byte[] encode(Object object) throws Exception {
		// TODO Auto-generated method stub
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		Output output = new Output(baos);
		KryoUtils.getKryo().writeClassAndObject(output, object);
		output.flush();
		output.close();
		byte[] b = baos.toByteArray();
		try {
			baos.flush();
			baos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return b;
	}

}
